"""
A class to exercise the JT core functions.
"""
import JT
from numpy import array, zeros, pi
import numpy as np
import JT.Data2 as Data


#Select data for test
vert = 0
proj = 1
body = 1

def Test():
    """
    Test the volume rendering implemented in JT
    """
#===============================================================================
#    Setup DRR
#===============================================================================
    # Load in data from an xml file
    data = Data.DataStructure()
    data_path = r'C:\Users\bryan\bryan-code\trunk\Tests\new_data_structure.xml' 
    data.load(data_path)

    drr = JT.DRR()

    Volume = data.Tree.Frame[vert].Volume[0]
    FixedImage = data.Tree.Frame[vert].FixedImage[proj]       
    
    # Load all of the data elements from the structure into the reg class
    drr.LoadCalFile(data.get_path(FixedImage.Cal))
    drr.LoadStaFile(data.get_path(Volume.STA))
    drr.SetVolumeFileName(data.get_path(Volume))
    drr.SetCenter(data.get_center(Volume, body))
    drr.renWin.SetSize(512,512)
    
    #Set the volume mapper to RayCast (more accurate, but slower)
    #Comment out to use TextureMapper
    drr.volume.UseRayCastMapper()
    drr.InteractionOff()
    #drr.renWin.SetOffScreenRendering(1)
  
    startPos = drr.volume.GetPosition()

    drr.Render()
    renderRate = zeros((2,20))
    for enum,i in enumerate(range(20)):
        drr.volume.SetPosition(startPos[0]+i,startPos[1],startPos[2])
        drr.Render()
        renderRate[0,enum] = 1/drr.GetRenderTime()
        print "%i Time to Render: %4.4f, Frame Rate: %4.2f " % (i, drr.GetRenderTime(), 
                                           1/drr.GetRenderTime())

    print "Ray-cast Results"
    print "    Average time: %4.2f +/- %2.2f, Average frame rate: %4.2f +/- %2.2f" % \
            (np.mean(1/renderRate[0,:]),np.std(1/renderRate[0,:]),
             np.mean(renderRate[0,:]),np.std(renderRate[0,:]))
    #Debugging, save the final generated image
    drr.SaveGeneratedImage('C:/Users/bryan/bryan-code/trunk/Images/DRR_RayCast.png')
    
    drr.volume.UseTextureMapper()
    drr.volume.SetPosition(startPos)
    drr.Render()
    for enum,i in enumerate(range(20)):
        drr.volume.SetPosition(startPos[0]+i,startPos[1],startPos[2])
        drr.Render()
        renderRate[1,enum] = 1/drr.GetRenderTime()
        print "%i Time to Render: %4.4f, Frame Rate: %4.2f " % (i, drr.GetRenderTime(), 
                                           1/drr.GetRenderTime())

    print "Texture Mapping Results"
    print "    Average time: %4.2f +/- %2.2f, Average frame rate: %4.2f +/- %2.2f" % \
            (np.mean(1/renderRate[1,:]),np.std(1/renderRate[1,:]),
             np.mean(renderRate[1,:]),np.std(renderRate[1,:]))
    #Debugging, save the final generated image
    drr.SaveGeneratedImage('C:/Users/bryan/bryan-code/trunk/Images/DRR_Texture.png')
    
    return renderRate


if __name__ == "__main__":
    Test()